<HTML>
<BODY BGCOLOR="white">
<PRE>
<FONT color="green">001</FONT>    // Copyright 2007 Waterken Inc. under the terms of the MIT X license<a name="line.1"></a>
<FONT color="green">002</FONT>    // found at http://www.opensource.org/licenses/mit-license.html<a name="line.2"></a>
<FONT color="green">003</FONT>    package org.waterken.bang;<a name="line.3"></a>
<FONT color="green">004</FONT>    <a name="line.4"></a>
<FONT color="green">005</FONT>    import static org.ref_send.test.Logic.join;<a name="line.5"></a>
<FONT color="green">006</FONT>    import static org.ref_send.test.Logic.was;<a name="line.6"></a>
<FONT color="green">007</FONT>    <a name="line.7"></a>
<FONT color="green">008</FONT>    import org.ref_send.list.List;<a name="line.8"></a>
<FONT color="green">009</FONT>    import org.ref_send.promise.Eventual;<a name="line.9"></a>
<FONT color="green">010</FONT>    import org.ref_send.promise.Promise;<a name="line.10"></a>
<FONT color="green">011</FONT>    <a name="line.11"></a>
<FONT color="green">012</FONT>    /**<a name="line.12"></a>
<FONT color="green">013</FONT>     * An introduction to eventual operations in Java.<a name="line.13"></a>
<FONT color="green">014</FONT>     * &lt;p&gt;<a name="line.14"></a>
<FONT color="green">015</FONT>     * This class provides an introduction to eventual operations by using them to<a name="line.15"></a>
<FONT color="green">016</FONT>     * update and query a counter held in an object of type {@link Drum}.<a name="line.16"></a>
<FONT color="green">017</FONT>     * &lt;/p&gt;<a name="line.17"></a>
<FONT color="green">018</FONT>     */<a name="line.18"></a>
<FONT color="green">019</FONT>    public final class<a name="line.19"></a>
<FONT color="green">020</FONT>    Beat {<a name="line.20"></a>
<FONT color="green">021</FONT>        private Beat() { /* no instance interface */ }<a name="line.21"></a>
<FONT color="green">022</FONT>        <a name="line.22"></a>
<FONT color="green">023</FONT>        /**<a name="line.23"></a>
<FONT color="green">024</FONT>         * Runs a unit test.<a name="line.24"></a>
<FONT color="green">025</FONT>         * &lt;p&gt;<a name="line.25"></a>
<FONT color="green">026</FONT>         * This method is called by the infrastructure code that manages the<a name="line.26"></a>
<FONT color="green">027</FONT>         * lifecycle of vats. The return from this method is an object that will be<a name="line.27"></a>
<FONT color="green">028</FONT>         * returned to the creator of the new vat. In this case, the vat creator<a name="line.28"></a>
<FONT color="green">029</FONT>         * will get a promise for a boolean.<a name="line.29"></a>
<FONT color="green">030</FONT>         * &lt;/p&gt;<a name="line.30"></a>
<FONT color="green">031</FONT>         * &lt;p&gt;<a name="line.31"></a>
<FONT color="green">032</FONT>         * By convention, an instance of {@link Eventual} is held in a variable<a name="line.32"></a>
<FONT color="green">033</FONT>         * named "_" and referred to as the "eventual operator". The eventual<a name="line.33"></a>
<FONT color="green">034</FONT>         * operator provides all eventual control flow operations, as well as the<a name="line.34"></a>
<FONT color="green">035</FONT>         * ability to produce "eventual references", which are references that<a name="line.35"></a>
<FONT color="green">036</FONT>         * schedule future invocation of a method, instead of invoking a method<a name="line.36"></a>
<FONT color="green">037</FONT>         * immediately. All references known to be eventual are also stored in<a name="line.37"></a>
<FONT color="green">038</FONT>         * variables whose name is suffixed with the '_' character. Consequently,<a name="line.38"></a>
<FONT color="green">039</FONT>         * you can scan down a page of code, looking for the character sequence "_."<a name="line.39"></a>
<FONT color="green">040</FONT>         * to find all the operations that are expected to be eventual.<a name="line.40"></a>
<FONT color="green">041</FONT>         * &lt;/p&gt;<a name="line.41"></a>
<FONT color="green">042</FONT>         * @param _     eventual operator<a name="line.42"></a>
<FONT color="green">043</FONT>         * @param drum  test subject<a name="line.43"></a>
<FONT color="green">044</FONT>         */<a name="line.44"></a>
<FONT color="green">045</FONT>        static public Promise&lt;?&gt;<a name="line.45"></a>
<FONT color="green">046</FONT>        make(final Eventual _, final Drum drum) {<a name="line.46"></a>
<FONT color="green">047</FONT>            /*<a name="line.47"></a>
<FONT color="green">048</FONT>             * First, ensure that we have an eventual reference to the drum, since<a name="line.48"></a>
<FONT color="green">049</FONT>             * the caller may have provided an immediate reference. The _()<a name="line.49"></a>
<FONT color="green">050</FONT>             * operation takes a reference that may be either immediate or eventual<a name="line.50"></a>
<FONT color="green">051</FONT>             * and returns a reference that is guaranteed to be eventual.<a name="line.51"></a>
<FONT color="green">052</FONT>             */ <a name="line.52"></a>
<FONT color="green">053</FONT>            final Drum drum_ = _._(drum);<a name="line.53"></a>
<FONT color="green">054</FONT>            <a name="line.54"></a>
<FONT color="green">055</FONT>            /*<a name="line.55"></a>
<FONT color="green">056</FONT>             * Start the test sequence by checking that the caller provided a new<a name="line.56"></a>
<FONT color="green">057</FONT>             * drum. We get the initial hit count by doing an eventual invocation of<a name="line.57"></a>
<FONT color="green">058</FONT>             * the getHits() method. If the provided drum is in another vat, this<a name="line.58"></a>
<FONT color="green">059</FONT>             * invocation will result in an HTTP GET request being sent to the<a name="line.59"></a>
<FONT color="green">060</FONT>             * hosting server. The return from the getHits() invocation is a promise<a name="line.60"></a>
<FONT color="green">061</FONT>             * for the number of hits. Using the when() operation, we register an<a name="line.61"></a>
<FONT color="green">062</FONT>             * observer on this promise, to receive a notification after the HTTP<a name="line.62"></a>
<FONT color="green">063</FONT>             * GET response has come back. The observer, constructed by the was()<a name="line.63"></a>
<FONT color="green">064</FONT>             * method, produces a promise for a boolean, indicating whether or not<a name="line.64"></a>
<FONT color="green">065</FONT>             * the number of hits specified in the HTTP GET response was the number<a name="line.65"></a>
<FONT color="green">066</FONT>             * expected. We'll hold onto this promise and use it to produce the<a name="line.66"></a>
<FONT color="green">067</FONT>             * promise returned to our caller.<a name="line.67"></a>
<FONT color="green">068</FONT>             */<a name="line.68"></a>
<FONT color="green">069</FONT>            final Promise&lt;?&gt; zero = _.when(drum_.getHits(), was(0));<a name="line.69"></a>
<FONT color="green">070</FONT>            <a name="line.70"></a>
<FONT color="green">071</FONT>            /*<a name="line.71"></a>
<FONT color="green">072</FONT>             * Increment the hit counter by doing an eventual invocation of<a name="line.72"></a>
<FONT color="green">073</FONT>             * the bang() method. If the provided drum is in another vat,<a name="line.73"></a>
<FONT color="green">074</FONT>             * this invocation will result in an HTTP POST request being<a name="line.74"></a>
<FONT color="green">075</FONT>             * sent to the hosting server.<a name="line.75"></a>
<FONT color="green">076</FONT>             */<a name="line.76"></a>
<FONT color="green">077</FONT>            drum_.bang(1);<a name="line.77"></a>
<FONT color="green">078</FONT>            <a name="line.78"></a>
<FONT color="green">079</FONT>            /*<a name="line.79"></a>
<FONT color="green">080</FONT>             * Requests sent on an eventual reference are sent in order, and<a name="line.80"></a>
<FONT color="green">081</FONT>             * so another check of the drum's hit count will see a value 1<a name="line.81"></a>
<FONT color="green">082</FONT>             * more than the previous check.<a name="line.82"></a>
<FONT color="green">083</FONT>             */<a name="line.83"></a>
<FONT color="green">084</FONT>            final Promise&lt;?&gt; one = _.when(drum_.getHits(), was(1));<a name="line.84"></a>
<FONT color="green">085</FONT>            <a name="line.85"></a>
<FONT color="green">086</FONT>            // We can queue up as many requests as we like...<a name="line.86"></a>
<FONT color="green">087</FONT>            drum_.bang(2);<a name="line.87"></a>
<FONT color="green">088</FONT>            <a name="line.88"></a>
<FONT color="green">089</FONT>            // ...and they will all be sent in order.<a name="line.89"></a>
<FONT color="green">090</FONT>            final Promise&lt;?&gt; three = _.when(drum_.getHits(), was(3));<a name="line.90"></a>
<FONT color="green">091</FONT>            <a name="line.91"></a>
<FONT color="green">092</FONT>            /*<a name="line.92"></a>
<FONT color="green">093</FONT>             * The Waterken server can log the causal chaining of all these<a name="line.93"></a>
<FONT color="green">094</FONT>             * events. Comments can be inserted into this log via the<a name="line.94"></a>
<FONT color="green">095</FONT>             * eventual operator.<a name="line.95"></a>
<FONT color="green">096</FONT>             */<a name="line.96"></a>
<FONT color="green">097</FONT>            _.log.comment("all bang requests queued");<a name="line.97"></a>
<FONT color="green">098</FONT>            <a name="line.98"></a>
<FONT color="green">099</FONT>            /*<a name="line.99"></a>
<FONT color="green">100</FONT>             * We now have 3 promises for checks on the expected value of the drum's<a name="line.100"></a>
<FONT color="green">101</FONT>             * hit count. We'll combine these 3 promises into 1 by doing an eventual<a name="line.101"></a>
<FONT color="green">102</FONT>             * join operation on them. The promise returned to our caller will<a name="line.102"></a>
<FONT color="green">103</FONT>             * resolve as soon as any one of the promises is rejected, or after all<a name="line.103"></a>
<FONT color="green">104</FONT>             * of them are fulfilled. Note that none of the HTTP requests have<a name="line.104"></a>
<FONT color="green">105</FONT>             * actually been sent yet; we've just scheduled them to be sent and<a name="line.105"></a>
<FONT color="green">106</FONT>             * setup code to be run when the responses eventually come back.<a name="line.106"></a>
<FONT color="green">107</FONT>             */<a name="line.107"></a>
<FONT color="green">108</FONT>            return join(_, zero, one, three);<a name="line.108"></a>
<FONT color="green">109</FONT>            <a name="line.109"></a>
<FONT color="green">110</FONT>            /*<a name="line.110"></a>
<FONT color="green">111</FONT>             * In total, we've scheduled 3 GET requests and 2 POST requests. If any<a name="line.111"></a>
<FONT color="green">112</FONT>             * of these communications are interrupted, due to a server crash or<a name="line.112"></a>
<FONT color="green">113</FONT>             * lost connection, the software will remember where it left off and<a name="line.113"></a>
<FONT color="green">114</FONT>             * resume as soon as network connections can be re-established, which<a name="line.114"></a>
<FONT color="green">115</FONT>             * the software will also periodically retry. Regardless of how often<a name="line.115"></a>
<FONT color="green">116</FONT>             * this process is interrupted, or for how long, the hit count on the<a name="line.116"></a>
<FONT color="green">117</FONT>             * drum will only be incremented by 3, the number specified by the<a name="line.117"></a>
<FONT color="green">118</FONT>             * algorithm above.<a name="line.118"></a>
<FONT color="green">119</FONT>             */<a name="line.119"></a>
<FONT color="green">120</FONT>        }<a name="line.120"></a>
<FONT color="green">121</FONT>        <a name="line.121"></a>
<FONT color="green">122</FONT>        // Command line interface<a name="line.122"></a>
<FONT color="green">123</FONT>    <a name="line.123"></a>
<FONT color="green">124</FONT>        /**<a name="line.124"></a>
<FONT color="green">125</FONT>         * Executes the test.<a name="line.125"></a>
<FONT color="green">126</FONT>         * &lt;p&gt;<a name="line.126"></a>
<FONT color="green">127</FONT>         * This class can also be run from the command line, to run tests against a<a name="line.127"></a>
<FONT color="green">128</FONT>         * local, transient {@link Drum}. Most factory classes won't provide a<a name="line.128"></a>
<FONT color="green">129</FONT>         * command line test suite and so won't have a {@link #main} method.<a name="line.129"></a>
<FONT color="green">130</FONT>         * &lt;/p&gt;<a name="line.130"></a>
<FONT color="green">131</FONT>         * @param args  ignored<a name="line.131"></a>
<FONT color="green">132</FONT>         * @throws Exception    test failed<a name="line.132"></a>
<FONT color="green">133</FONT>         */<a name="line.133"></a>
<FONT color="green">134</FONT>        static public void<a name="line.134"></a>
<FONT color="green">135</FONT>        main(final String[] args) throws Exception {<a name="line.135"></a>
<FONT color="green">136</FONT>            /*<a name="line.136"></a>
<FONT color="green">137</FONT>             * All the eventual control flow operations bottom out in runnable tasks<a name="line.137"></a>
<FONT color="green">138</FONT>             * on an event loop. This method provides a simple implementation for<a name="line.138"></a>
<FONT color="green">139</FONT>             * the event loop. The Waterken Server provides a more complete<a name="line.139"></a>
<FONT color="green">140</FONT>             * implementation that supports multiple concurrent event loops with<a name="line.140"></a>
<FONT color="green">141</FONT>             * transparent persistence and across the network messaging.<a name="line.141"></a>
<FONT color="green">142</FONT>             */<a name="line.142"></a>
<FONT color="green">143</FONT>            final List&lt;Promise&lt;?&gt;&gt; work = List.list();<a name="line.143"></a>
<FONT color="green">144</FONT>            final Eventual _ = new Eventual(work.appender());<a name="line.144"></a>
<FONT color="green">145</FONT>            final Promise&lt;?&gt; result = make(_, Bang.make());<a name="line.145"></a>
<FONT color="green">146</FONT>            while (!work.isEmpty()) { work.pop().call(); }<a name="line.146"></a>
<FONT color="green">147</FONT>            result.call();<a name="line.147"></a>
<FONT color="green">148</FONT>        }<a name="line.148"></a>
<FONT color="green">149</FONT>    }<a name="line.149"></a>




























































</PRE>
</BODY>
</HTML>
